﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CoolERP_BLL;
using CoolERP_Entities;

namespace CoolERP_Web.User
{
    public partial class RoleAssign : BasePage
    {
        protected Guid UserID
        {
            get { return new Guid(this.GetUrlParam("id").ToString()); }
        }
        protected List<string> userRoles = new List<string>();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["name"] != null)
                    this.lbTitle.Text = "分配角色到用户：" + Request.QueryString["name"];
                else
                    this.lbTitle.Text = "分配角色到用户";
                this.AspNetPager1.PageSize = GridView1.PageSize;
                BindGridview();
            }
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string rid = GridView1.DataKeys[e.Row.RowIndex].Value.ToString().Trim();
                CheckBox cb = e.Row.FindControl("cbAssign") as CheckBox;
                cb.Checked = userRoles.Contains(rid);
                cb.Attributes.Add("id", rid);
            }
        }

        private void BindGridview()
        {
            GetUserRoles();
            int RecordCount = 0;
            using (RolesBLL rBLL = new RolesBLL())
            {
                List<Roles> itemLists = rBLL.GetPagerList(null, "Name asc", this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize, out RecordCount);
                this.AspNetPager1.RecordCount = RecordCount;
                GridView1.DataSource = itemLists;
                GridView1.DataBind();
            }
            this.AspNetPager1.Visible = RecordCount > 0;
            panMsg.Visible = !this.AspNetPager1.Visible;
        }

        private void GetUserRoles()
        {
            using (UserInRolesBLL userInRolesB = new UserInRolesBLL())
            {
                List<UserInRoles> list = userInRolesB.GetList(t => t.UserId == UserID);
                userRoles = list.Select(t => t.RoleId.ToString()).ToList();
            }
        }

        protected void cbAssign_CheckedChanged(object sender, EventArgs e)
        {
            string resultMessage;
            CheckBox cb = (sender as CheckBox);
            Guid rid = Guid.Parse(cb.Attributes["id"]);
            bool res = true;
            using (UserInRolesBLL uBLL = new UserInRolesBLL())
            {
                if (cb.Checked)
                {
                    UserInRoles entity = new UserInRoles();
                    entity.RoleId = rid;
                    entity.UserId = UserID;
                    entity.Id = Guid.NewGuid();
                    res = uBLL.Insert(entity);
                }
                else
                {
                    UserInRoles entity = uBLL.GetEntity(t => t.RoleId == rid && t.UserId == UserID);
                    res = uBLL.Delete(entity);
                }
            }
            if (res == true)
                resultMessage = "更新用户角色成功";
            else
                resultMessage = "更新用户角色失败";
            lbResult.Text = resultMessage;
            BindGridview();
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindGridview();
        }
    }
}